package com.mofang.powerdekorhelper.persenter;

import com.mofang.powerdekorhelper.base.BasePresent;
import com.mofang.powerdekorhelper.utils.UrlTools;
import com.mofang.powerdekorhelper.utils.http.InitRetrofit;
import com.mofang.powerdekorhelper.utils.http.RetrofitSerives;
import com.mofang.powerdekorhelper.view.BindPhoneVerifyView;

import org.json.JSONException;
import org.json.JSONObject;

import okhttp3.RequestBody;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

/**
 * Created by luofei on 2017/8/19 0019.
 */

public class BindPhoneVerifyPerenster extends BasePresent<BindPhoneVerifyView> {
    @Override
    public void stopRequest() {

    }

    /**
     * 获取短信验证码
     *
     * @param phone
     */
    public void getMesCode(String phone) {

        JSONObject object = new JSONObject();
        try {
            object.put("phone", phone);
        } catch (JSONException e) {
            e.printStackTrace();
        }

        final String requestStr = object.toString();

        RequestBody body = RequestBody.create(okhttp3.MediaType.parse("application/json; charset=utf-8"), requestStr);

        RetrofitSerives requestSerives = InitRetrofit.getInstance().getRetrofit(UrlTools.Services);

        Call<String> call = requestSerives.getMesvcode(body);

        call.enqueue(new Callback<String>() {
            @Override
            public void onResponse(Call<String> call, Response<String> response) {
                if (response.isSuccessful()) {
                    view.setVCodeResult(response.body());
                } else {
                    view.onError("请求失败，请重试");
                }
            }

            @Override
            public void onFailure(Call<String> call, Throwable t) {
                view.onError("请求服务器异常，稍候再试" + t.getMessage());
            }
        });
    }

    public void getBindPhoneVerify(int userId, String vcode, String phone, int flag) {

        JSONObject object = new JSONObject();

        try {
           // object.put("id", id);
            object.put("vcode", vcode);
            object.put("phone", phone);
        } catch (JSONException e) {
            e.printStackTrace();
        }

        final String requestStr = object.toString();

        final RequestBody body = RequestBody.create(okhttp3.MediaType.parse("application/json; charset=utf-8"), requestStr);

        RetrofitSerives requestSerives = InitRetrofit.getInstance().getRetrofit(UrlTools.Services);

        Call<String> call = requestSerives.getChangeBindPhone(body);

        call.enqueue(new Callback<String>() {
            @Override
            public void onResponse(Call<String> call, Response<String> response) {
                if (response.isSuccessful()) {
                    view.setVerifyResult(response.body());
                } else {
                    view.onError("请求失败，请重试");
                }
            }

            @Override
            public void onFailure(Call<String> call, Throwable t) {
                view.onError("请求服务器异常，稍候再试" + t.getMessage());
            }
        });

    }
}
